import { createAction } from 'redux-actions'
import { check, body, delJson, getJson, postJson, putJson, parseJSON, checkStatus, data } from './util'
import util from '../util'

/** New Action */
export
const fetchTeamMemeber = createAction('fetch team memeber', (teamId, cb) =>
  fetch(`/teams/${teamId}/subordinate`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export 
const setTeamAdmin = createAction('_', (arg,cb) =>
  fetch('/roles/set-up-team-admin', {
    ...postJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)

export
const fetchTeams = createAction('fetch teams', (arg, cb) =>
  fetch('/teams', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export
const createTeam = createAction('create team', (arg, cb) =>
  fetch('/teams', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      if (cb) {
        cb({
          ...arg,
          teamId: res.data.idValue
        })
      }
    })
)

export
const deleteTeam = createAction('delete team', (teamId, cb) =>
  fetch(`/teams/${teamId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(res => {
      cb(teamId,res)
    })
)

export
const deleteTeamHasChildren = createAction('delete team has children', (teamId, cb) =>
  fetch(`/teams_project/${teamId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb(teamId,res)
    })
)


//修改
export
const updateTeam = createAction('update team', (teamId, arg, cb) => {
  fetch(`/teams/${teamId}`, {
    ...putJson,
    ...body(arg),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
})

export
const moveTeam = createAction('move team', (arg, cb) =>
  fetch('/teams/transfer', {
    ...putJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

/** End Of New Action */


export 
const fetchTeamData = createAction('fetch team data', () =>
	fetch('/teams', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export
const clearTeam = createAction('clear team')

export
const clearTeamSubordinates = createAction('clear team subordinates')


export
const clearSubordinate = createAction('clear subordinate')

export
const fetchReportees = createAction('fetch reportees', userId =>
	fetch(`/report-lines/reportees/${userId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => ({
      leader: userId,
      reportees: res.data
    }))
)

export
const clearReportees = createAction('clear reportees')

export
const clearTeamReportees = createAction('clear team reportees')

export
const updateReportees = createAction('update reportees', (arg, cb) =>
	fetch('/report-lines', {
    ...putJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export
const reloadReportees = createAction('reload reportees', userId => 
	fetch(`/report-lines/reportees/${userId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => ({
      leader: userId,
      reportees: res.data
    }))
)

export
const fetchTeamAncestorChildren = createAction('fetch team ancestor children', (teamId, cb) =>
	fetch(`/teams/${teamId}/ancestor-children`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      if (cb) {
        cb(res);
        return 'PASS';
      } else {
        return res.data;
      }
    })
)

export
const fetchTeamUsers = createAction('fetch team users', arg => { 
  const { teamId, ...others } = arg
  return (
    fetch(`/teams/${teamId}/users?${util.body(others)}`, getJson)
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(data)
  )
})

export
const fetchTeam = createAction('fetch team', (teamId, cb) =>
	fetch(`/teams/${teamId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(data)
)

// 获取汇报对象
export
const fetchReportor = createAction('fetch reportor', (userId, cb) =>
	fetch(`/teams/${teamId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(res => {
			cb(res.data)
		})
)

export
const resetTeam = createAction('reset team')
